<?php
class BannersController extends LiladsAppController {
    var $name = 'Banners';
	var $uses = array('Lilads.Banner', 'Lilads.Campaign');

	function click($id=null) {
		if (!is_numeric($id)) {
			$this->cakeError('error404');
		}
		$redirect = $this->Banner->click($id);
		if (!empty($this->params['named']['link'])) {
			$redirect = urldecode($this->params['named']['link']);
			if (substr($redirect, 0, 4) != 'http') $redirect = 'http://'.$redirect;
		}
		if (!empty($redirect)) {
			$this->redirect($redirect);
		} else {
			$this->cakeError('error404');
		}
	}
	
	function display($id=null) {
		if (is_numeric($id) && $banner = $this->Banner->read(null, $id)) {
			$this->layout = false;
			Configure::write('debug', 0);
			$this->autoRender = false;
			
			$filename = Configure::read('Lilads.uploadFolder').$banner['Banner']['filename'];
			switch ($banner['Banner']['kind']) {
				case 'I':
					ob_end_clean();
					$content_types = array('gif'=>'image/gif', 'png'=>'image/png', 'jpg'=>'image/jpeg', 'jpeg'=>'image/jpeg');
					header('Content-Transfer-Encoding: binary');
					header('Last-Modified: '.date('r'));
					header('Content-Length: '.filesize($filename));
					header('Content-Type: '.$content_types[strtolower(substr($filename, strrpos($filename, '.') + 1))]);
					
					readfile($filename);
					exit;
					break;
				case 'F':
					ob_end_clean();
					header('Content-Transfer-Encoding: binary');
					header('Last-Modified: '.date('r'));
					header('Content-Type: application/x-shockwave-flash');
					header('Content-Length: '.filesize($filename));
					readfile($filename);
					exit;
					break;
				case 'P':
					echo h($banner['Banner']['body']);
					break;
				case 'H':
					echo $banner['Banner']['body'];
					break;
			}
		} else {
			$this->cakeError('error404');
		}
	}

	function admin_add($id=null) {
		$this->setAction('admin_edit');
	}

	function admin_edit($id = null) {
		if (!empty($this->data)) {
			if ($this->Banner->save($this->data)) {
				$this->Session->setFlash(__('Ad has been saved.', true));
				
				if (!empty($this->data['Banner']['referer'])) {
					$this->redirect($this->data['Banner']['referer']);
				} else {
					$this->redirect(array('action'=>'index'));
				}
			} else {
				$this->Session->setFlash(__('Please verify that the information is correct.', true), null, null, 'error');
			}
		}
		if (empty($this->data)) {
			if (is_numeric($id)) {
				$this->data = $this->Banner->read(null, $id);
			} else {
				if (!empty($this->params['named']['campaign']) && is_numeric($this->params['named']['campaign'])) {
					$this->data['Banner']['campaign_id'] = $this->params['named']['campaign'];
				} else {
					$campaigns = $this->Campaign->find('list', array('conditions'=>array('active'=>1), 'order'=>'title'));
					$this->set(compact('campaigns'));
				}
				$this->data['Banner']['kind'] = 'I';
			}
			$this->data['Banner']['referer'] = $this->referer();
		}
	}

	function admin_delete($id = null) {
		if (is_numeric($id) && $this->Banner->del($id)) {
			$this->Session->setFlash(__('Ad has been deleted.', true));
			$this->redirect($this->referer());
		} else {
			$this->cakeError('error404');
		}
	}
}
?>